(function($) {
	var url = location.href.split("/");
	var baseUrl = "http://" + url[2] + "/" + url[3];
	var pageNo;
	var pageSize;
	var data;
	var buildHtml = function($this,$ths){
		var tds = "<tbody class=\"text-c\">";
		if(data){
			var length = data.length;
			for(var i = 0 ; i < length ; i++){
				tds += "<tr>"
				var size = $ths.length;
				for(var j = 0 ; j < size ; j++){
					var style = $ths[j].style;
					tds += "<td  style = '" + style + "'>";
					var type = $ths[j].type;
					var value = "";
					var names = $ths[j].name.split(".");
					/*if(names.length > 1){
						value = data[i][names[0]]?data[i][names[0]][names[1]]:"";
					}else{
						value = data[i][names[0]];*/
						value = data[i][names[0]]?names.length > 1?data[i][names[0]][names[1]]?data[i][names[0]][names[1]]:"":data[i][names[0]]?data[i][names[0]]:"":"";
					/*}*/
					//value = names.length > 1?data[i][names[0]][names[1]]?data[i][names[0]][names[1]]:"":data[i][names[0]]?data[i][names[0]]:"";
					//
					//value = value?value:"";
					if(type == "dict"){
						var dictType = $ths[j].dictType;
						value = getDictLable(dictType,value);
						value = value?value:"";
					}else if(type == "Integer"){
						value = value == 0?"0":value;
					}
					tds += value + "</td>";
				}
				tds += "</tr>";
			}
		}
		tds += "</tbody>";
		$this.append(tds);
		//body end
	}
	$.fn.getCheckData = function(){
		var $this = $(this);
		var res = new Array();
		var radios = $this.find("tbody td input[type='checkbox']");
		var l = radios.length;
		for(var i = 0 ; i < l ; i ++){
			if($(radios[i]).is(":checked")) res.push(data[i]);
		}
		return res;
	}
    $.fn.getAllData = function () {
		return data;
    }
	$.fn.ECDATable = function(list) {
		var $this = $(this);
		var $ths = [];
		var id = $this.attr("id");
		var paramdata = {};
		var indexflag = $this.attr("indexflag") == "true";
		if(!pageNo){
			pageNo = 1;
		}
		paramdata["pageNo"] = pageNo;
		var operate = $this.attr("operate") == "true";
		var cachehtml = $this.children("tbody[style='display:none;']").html();
		$this.empty();
		$this.parent().find(".table-pages").remove();
		cachehtml = "<tbody style = 'display:none;'>" + cachehtml + "</tbody>";
		$this.append(cachehtml);
		var html = "<thead class=\"text-c\"><tr>";
		if(operate){
			html += "<td style = 'width:80px'>操作</td>";
		}
		$this.find("th").each(function(){
			var $th = {};
			$th.name = $(this).attr("name");
			$th.style = $(this).attr("style");
			$th.type = $(this).attr("type");
			$th.dictType = $(this).attr("dictType");
			$th.title = $(this).attr("title");
			$ths.push($th);
			html += "<td style = '" + $th.style + "'>" + $th.title + "</td>"; 
		});
		html += "</tr></thead>";
		$this.prepend(html);
	    var t = $("#" + $this.attr("bindForm")).serializeArray();
	    $.each(t, function() {
	    	paramdata[this.name] = this.value;
	    });
	    var dataAction = $this.attr("dataAction");
	    var type = $this.attr("type");
	    var url = baseUrl + dataAction;
	    if(!dataAction){
	    	data = list;
	    	buildHtml($this,$ths);
	    	if(tableCallBack&&typeof tableCallBack == "function"){
    	    	tableCallBack($this);
    	    }
	    	return;
	    }
	    jQuery.ajax({
	        url: url,
	        dataType: 'json',
	        data: paramdata,
	        timeout: 60000,
	        cache: false,
	        async: true,
	        success: function(page) {
	        	//body begin
	        	pageSize = page.pageSize;
	    		pageNo = page.pageNo;
	    		pageCount = page.count%pageSize == 0?page.count/pageSize:(page.count/pageSize+1);
	    		data = page.list ? page.list : page.records;
	    		buildHtml($this,$ths);
	    		//Page begin

	    		if($this.attr("pagenation") != "false"){
	    			$this.parent().append(page.html);
	    		}
	    	    $this.next().find("a").click(function(){
	    	    	var dataPage = $(this).attr("data-page");
	    	    	var text = $(this).text();
	    	    	if(!isNaN(dataPage)){
	    	    		pageNo = $(this).attr("data-page");
	    	    	}else if(pageNo>1&&dataPage == "pre"){
	    	    		pageNo--
	    	    	}else if(pageNo<pageCount&&dataPage == "next"){
	    	    		pageNo++;
	    	    	}else if(text == "跳转"){
	    	    		dataPage = $(this).prev().val();
	    	    		if(1 <= dataPage <= pageCount){
	    	    			pageNo = dataPage;
	    	    		}else{
	    	    			return;
	    	    		}
	    	    	}else{
	    	    		return;
	    	    	}
	    	    	$("#" + $this.attr("bindClick")).click();
	    	    });
	    	    $this.find("th input[type='checkbox']").click(function(){
	    	    	$this.find("td input[type='checkbox']").prop("checked",$(this).is(":checked"));
	    	    });
	    	    if(tableCallBack&&typeof tableCallBack == "function"){
	    	    	tableCallBack($this);
	    	    }
	        }
	    });
	};  
})(jQuery);


function getDictValue(status,type,callback){
	var data = {};
	data.values = status;
	data.type = type;
	data.defaultValue='';
	jQuery.ajax({
        url: baseUrl+'/dict/getDictLabels',
        timeout: 60000,
        cache: false,
        data:data,
        success: function(datas) {
        	callback(datas);
        }
     })
}

$(document).ready(function() {
	var $table = $('table[datatable="true"]');
	$table.each(function(){
		var table = $(this).ECDATable();
		var $tableClick = $("#" + $(this).attr("bindClick"));
		$tableClick.click(function(){
			$('table[datatable="true"][bindClick="'+ $(this).attr("id") +'"]').ECDATable();
		});
	})
})





